package app.itgungnir.compose.wan.business.main

import androidx.lifecycle.ViewModel
import app.itgungnir.compose.wan.utils.LocalDataStore
import app.itgungnir.compose.wan.utils.launch
import app.itgungnir.compose.wan.utils.wanAndroidService
import app.itgungnir.compose.wan.utils.wanResponse
import kotlinx.coroutines.flow.MutableStateFlow

class MainViewModel : ViewModel() {

    val unreadMsgCountState = MutableStateFlow(0)

    init {
        getUnreadMessageCount()
    }

    fun getUnreadMessageCount() = launch {
        if (!LocalDataStore.localData.hasLogin) {
            return@launch
        }
        wanAndroidService.getUnreadMessageCount().wanResponse<Int>()?.let {
            unreadMsgCountState.value = it
        }
    }
}
